package com.deyou.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by yangyuancheng on 2017/6/8.
 *
 * 利用SharePreferences保存数值
 */

public class SPUtils {

    private static SharedPreferences mShareConfig;

    /**
     * 保存String值
     *
     * @param key
     * @param value
     */
    public static void saveStringByKey(Context context, String key, String value) {
        if (!TextUtils.isEmpty(key) && context != null) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {

                final SharedPreferences.Editor edit = mShareConfig.edit();
                try {
                    if (!TextUtils.isEmpty(value))
                        value = SimpleCrypto.encrypt(key, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                edit.putString(key, value);
                edit.commit();
            }
        }
    }

    /**
     * 根据key得到信息
     *
     * @param key
     */
    public static String getStringByKey(Context context, String key) {
        String value = null;
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                value = mShareConfig.getString(key, "");
            }
        }
        try {
            return SimpleCrypto.decrypt(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return value;
        }

    }

    /**
     * 保存Int值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveIntByKey(Context context, String key, int value) {
        if (!TextUtils.isEmpty(key) && null != context) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                final SharedPreferences.Editor edit = mShareConfig.edit();
                edit.putInt(key, value);
                edit.commit();
            }
        }
    }

    /**
     * 根据key得到信息
     *
     * @param key
     */
    public static int getIntByKey(Context context, String key) {
        int value = 0;
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                value = mShareConfig.getInt(key, 0);
            }
        }
        return value;
    }

    /**
     * 保存long值
     *
     * @param context
     * @param key
     * @param t
     */
    public static void saveLongByKey(Context context, String key, long t) {
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                final SharedPreferences.Editor edit = mShareConfig.edit();
                edit.putLong(key, t);
                edit.commit();
            }
        }
    }

    /**
     * 根据key得到long信息
     *
     * @param key
     */
    public static long getLongByKey(Context context, String key) {
        long value = -1l;
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                value = mShareConfig.getLong(key, -1l);
            }
        }
        return value;
    }

    /**
     * 保存long值
     *
     * @param context
     * @param key
     * @param t
     */
    public static void saveFloatByKey(Context context, String key, float t) {
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                final SharedPreferences.Editor edit = mShareConfig.edit();
                edit.putFloat(key, t);
                edit.commit();
            }
        }
    }

    /**
     * 根据key得到long信息
     *
     * @param key
     */
    public static float getFloatByKey(Context context, String key) {
        float value = -1l;
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                value = mShareConfig.getFloat(key, -1l);
            }
        }
        return value;
    }

    /**
     * 保存boolean值
     *
     * @param key
     * @param value
     */
    public static boolean saveBooleanByKey(Context context, String key, boolean value) {
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                final SharedPreferences.Editor edit = mShareConfig.edit();
                edit.putBoolean(key, value);
                edit.commit();
            }
        }
        return value;
    }

    /**
     * 根据key得到boolean信息
     *
     * @param key
     */
    public static boolean getBooleanByKey(Context context, String key) {
        boolean value = false;
        if (!TextUtils.isEmpty(key)) {
            mShareConfig = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
            if (null != mShareConfig) {
                value = mShareConfig.getBoolean(key, false);
            }
        }
        return value;
    }
}
